(function () {
    'use strict';

    angular.module('KingAdmin.pages.sys.job')
        .factory('JobService', JobService);

    /** @ngInject */
    function JobService($resource,toastr,CommonService) {

        var rest = $resource('sys/job/:id', {}, {
            'create': {method: 'POST'},
            'update': {method: 'PUT'},
        });
        
        function getSmartData(param,callback) {
            $resource('sys/job/getSmartData', {}, {
                'query': {method: 'POST'}
            }).query(param,
                function (data,hears) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);callback(data)
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }
        function del(param,callback) {
            CommonService.danger('确定删除?', function () {
                // rest.delete(param,
                //     function (data) {
                //         if (data.code == 0) {
                //             toastr.success("删除成功！", "提示", {"progressBar": true,});
                //         } else {
                //             toastr.warning("删除失败！", "提示", {"progressBar": true,});
                //         }
                //         if(angular.isFunction(callback)) callback(data);
                //     }, function (error) {
                //         toastr.error(error, "提示", {"progressBar": true,});
                //     })

                $resource('sys/job/delete/:id', {id: param.id}, {
                    'delete': {method: 'DELETE'}
                }).delete({},
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("删除成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning("删除失败！", "提示", {"progressBar": true,});
                        }
                        if(angular.isFunction(callback)) callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    });
            })
        }
        function save(param,callback) {
            CommonService.info('确定保存?',function () {
                if(angular.isDefined(param.id)&&param.id!=null){
                    // rest.update(param,
                    //     function (data) {
                    //         console.log(data);
                    //         toastr.success("保存成功","提示",{"progressBar": true,});
                    //         if(angular.isFunction(callback)) callback(data);
                    //     }, function (error) {
                    //         toastr.error(error,"提示",{"progressBar": true,});
                    //     })

                    $resource('sys/job/update/:id', {id: param.id}, {
                        'update': {method: 'PUT'}
                    }).update(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        });
                }else{
                    // rest.create(param,
                    //     function (data) {
                    //         console.log(data);
                    //         toastr.success("保存成功","提示",{"progressBar": true,});
                    //         if(angular.isFunction(callback)) callback(data);
                    //     }, function (error) {
                    //         toastr.error(error,"提示",{"progressBar": true,});
                    //     })

                    $resource('sys/job/save', {}, {
                        'add': {method: 'POST'}
                    }).add(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            if(angular.isFunction(callback)) callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        });
                }
            });
        }
        function getInfo(param,callback) {
            rest.get(param,
                function (data) {
                    console.log(data);
                    if(angular.isFunction(callback)) callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        function pauseJob(jobId, callback) {
            CommonService.danger('确定暂停该任务?', function () {
                $resource('sys/job/pause/:id', {id: jobId}, {
                    'pause': {method: 'GET'}
                }).pause({},
                    function (data) {
                        if (data.code === 0) {
                            toastr.success("暂停任务成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning("暂停任务失败！", "提示", {"progressBar": true,});
                        }
                        if (angular.isFunction(callback)) callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    });
            });
        }

        function resumeJob(jobId, callback) {
            CommonService.info('确定恢复该任务?', function () {
                $resource('sys/job/resume/:id', {id: jobId}, {
                    'resume': {method: 'GET'}
                }).resume({},
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("恢复任务成功！", "提示", {"progressBar": true,});
                        } else {
                            toastr.warning("恢复任务失败！", "提示", {"progressBar": true,});
                        }
                        if (angular.isFunction(callback)) callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    });
            });
        }
        
        return {
            getSmartData:getSmartData,
            del:del,
            save:save,
            getInfo:getInfo,
            pauseJob: pauseJob,
            resumeJob: resumeJob,
        };

    }

})();
